﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BrainTechLLC.FolderExplorer
{
    /// <summary>
    /// Interaction logic for ShowFolderListView.xaml
    /// </summary>
    public partial class ShowFolderListView : UserControl
    {
        public static readonly DependencyProperty FolderProperty =
                   DependencyProperty.Register("Folder", typeof(FolderViewModel), typeof(ShowFolderListView), new PropertyMetadata(new FolderViewModel(null, null) { FullPath = @"C:\" }, OnFolderPropertyChanged));

        public FolderViewModel Folder
        {
            get { return (FolderViewModel)GetValue(FolderProperty); }
            set { SetValue(FolderProperty, value); }
        }

        private static void OnFolderPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ShowFolderListView source = d as ShowFolderListView;
            source.UpdateFolder(e.NewValue as FolderViewModel, e.OldValue as FolderViewModel);
        }

        private void UpdateFolder(FolderViewModel f, FolderViewModel old)
        {            
            if (old != null)
            {
                old.AbortCalcDirSize();
            }

            this.topGrid.DataContext = f;

            if (f != null)
            {
                f.CalcDirSizeAsync();
            }
        }

        public static readonly DependencyProperty TextSizeProperty =
            DependencyProperty.Register("TextSize", typeof(double), typeof(ShowFolderListView), new PropertyMetadata(9.0, OnTextSizePropertyChanged));

        public double TextSize
        {
            get { return (double)GetValue(TextSizeProperty); }
            set { SetValue(TextSizeProperty, value); }
        }

        private static void OnTextSizePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ShowFolderListView source = d as ShowFolderListView;
            double v = (double)e.NewValue;
            source.UpdateTextSize(v);
        }

        private void UpdateTextSize(double val)
        {
            UpdateUI();
        }

        public void UpdateUI()
        {
            listView1.SetValue(TextBlock.FontSizeProperty, TextSize);
        }

        public ShowFolderListView()
        {
            InitializeComponent();
        }
    }
}
